<template>
  <div>
    <el-dialog
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      :title="title"
      :append-to-body="true"
      :visible="innerDialogFormVisible"
      :before-close="handleClose"
      @closed="closedDialog"
    >
      <p slot="title">{{ title }}</p>

      <el-form
        ref="formData"
        class="base-pipeline-layout system-detectionitem-form"
        :model="formData"
        :rules="formDataRules"
        status-icon
        label-width="110px"
      >
        <el-form-item
          :label="sysBusinessDetectionItem.sampleCategory"
          prop="sysBusinessSampleCategoryId"
          :error="errorMsg.sysBusinessSampleCategoryId"
        >
          <BaseTreeSelect
            ref="TreeSelect"
            v-model="formData.sysBusinessSampleCategoryId"
            node-key="id"
            :tree-data.sync="treeData"
          />
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.itemParameterName"
          prop="detectionItemName"
          :error="errorMsg.detectionItemName"
        >
          <el-input
            v-model="formData.detectionItemName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.productStandards"
          prop="productStandardName"
        >
          <el-input
            v-model="formData.productStandardName"
            style="width: 250px"
            disabled
          ></el-input>
          <div
            v-if="formData.productStandardName"
            class="emptyProductStandard"
            @click="emptyProductStandard"
          >
            -
          </div>
          <div
            v-else
            class="selectProductStandard"
            @click="selectProductStandard"
          >
            +
          </div>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.methodStandards"
          prop="methodStandardName"
        >
          <el-input
            v-model="formData.methodStandardName"
            style="width: 250px"
            disabled
          ></el-input>
          <div
            v-if="formData.methodStandardName"
            class="emptyProductStandard"
            @click="emptyMethodStandard"
          >
            -
          </div>
          <div
            v-else
            class="selectProductStandard"
            @click="selectMethodStandard"
          >
            +
          </div>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.projectSubItems"
          prop="detectionItemChildEnable"
        >
          <el-switch
            v-model="formData.detectionItemChildEnable"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item
          v-if="formData.detectionItemChildEnable != 1"
          :label="sysBusinessDetectionItem.symbol"
          prop="unitSymbol"
        >
          <el-input
            v-model="formData.unitSymbol"
            style="width: 250px"
            disabled
          ></el-input>
          <div
            v-if="formData.unitSymbol"
            class="emptyProductStandard"
            @click="emptyUnit"
          >
            -
          </div>
          <div v-else class="selectProductStandard" @click="selectUnit">+</div>
        </el-form-item>
        <el-form-item
          v-if="formData.detectionItemChildEnable != 1"
          :label="sysBusinessDetectionItem.judgmentSymbol"
          prop="decisionSymbol"
        >
          <el-select
            v-model="formData.decisionSymbol"
            :placeholder="operateType.pleaseSelect"
            @change="decisionSymbolChange"
          >
            <el-option
              key=""
              :label="sysBusinessDetectionItem.noJudgmentSymbol"
              value=""
            ></el-option>
            <el-option
              v-for="item in decisionSymbolList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="formData.detectionItemChildEnable != 1"
          :label="sysBusinessDetectionItem.qualityIndex"
          prop="qualityIndex"
        >
          <el-input
            v-model="formData.qualityIndex"
            :placeholder="qualityIndexPlaceholder"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.commonDetectionValues"
          prop="detectionLimitList"
        >
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          ></el-input>
          <el-button
            v-else
            class="button-new-tag"
            size="small"
            @click="showInput"
            >+ New Tag</el-button
          >
          <el-tag
            v-for="tag in formData.detectionLimitList"
            :key="tag.id"
            closable
            :disable-transitions="false"
            @close="handleCloseTag(tag)"
            >{{ tag.name }}</el-tag
          >
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.sampleValidityPeriod"
          prop="sampleValidity"
        >
          <el-input
            v-model="formData.sampleValidity"
            style="width: 70%"
            @blur="blurTexts($event)"
          ></el-input>
          <el-select
            v-model="formData.validityUnit"
            style="width: 30%"
            :placeholder="operateType.pleaseSelect"
          >
            <el-option
              key=""
              :label="sysBusinessDetectionItem.noExpirationDate"
              :value="0"
            ></el-option>
            <el-option
              v-for="item in validityUnitOptions"
              :key="Number(item.value)"
              :label="item.name"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.assemblyTestingPersonnel"
          prop="detectionItemChildEnable"
        >
          <!-- <BaseTreeRelevance
            ref="BaseTreeRelevance"
            v-model="formData.detectionPeopleList"
            node-key="id"
            :tree-data.sync="detectionPeopleOptions"
          /> -->
          <el-input
            v-model="formData.detectionPeopleLabelList"
            style="width: 250px"
            disabled
          ></el-input>
          <div class="selectProductStandard" @click="selectUser">+</div>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.capacityRange"
          prop="scopeAccreditation"
        >
          <el-select
            v-model="formData.scopeAccreditation"
            :placeholder="operateType.pleaseSelect"
          >
            <el-option
              key=""
              :label="sysBusinessDetectionItem.noAbilityRange"
              :value="0"
            ></el-option>
            <el-option
              v-for="item in scopeAccreditationOptions"
              :key="Number(item.value)"
              :label="item.name"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.limits"
          prop="detectionItemChildEnable"
        >
          <el-input v-model="formData.restrictRange"></el-input>
        </el-form-item>
        <el-form-item
          :label="sysBusinessDetectionItem.itemType"
          prop="itemType"
        >
          <el-select
            v-model="formData.itemType"
            :placeholder="operateType.pleaseSelect"
          >
            <el-option
              key=""
              :label="sysBusinessDetectionItem.noItemType"
              value="0"
            ></el-option>
            <el-option
              v-for="item in itemTypeOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div v-if="formData.detectionItemChildEnable == 1">
        <span>{{ sysBusinessDetectionItem.projectSubItems }}</span>
        <div>
          <el-button
            icon="el-icon-plus"
            type="primary"
            plain
            :disabled="readOnly"
            @click="addDetection"
            >{{ operateType.add }}</el-button
          >
          <el-button
            icon="el-icon-document-copy"
            type="primary"
            plain
            :disabled="readOnly"
            @click="copyDetection"
            >{{ operateType.copy }}</el-button
          >
          <el-button
            icon="el-icon-delete"
            type="danger"
            plain
            :disabled="readOnly"
            @click="deleteDetection"
            >{{ operateType.delete }}</el-button
          >
          <el-table
            ref="table"
            border
            :data="formData.itemSubentryDtos"
            style="width: 100%"
            height="180px"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column
              prop="detectionItemName"
              :label="sysBusinessDetectionItem.projectSubItems"
            />
            <el-table-column
              prop="unitSymbol"
              :label="sysBusinessDetectionItem.symbol"
            />
            <el-table-column
              prop="decisionSymbol"
              :label="sysBusinessDetectionItem.judgmentSymbol"
            />
            <el-table-column
              prop="qualityIndex"
              :label="sysBusinessDetectionItem.qualityStandard"
            />
            <el-table-column :label="operateType.action">
              <template slot-scope="scope">
                <el-link type="primary" @click="editUnit(scope.row)">{{
                  operateType.redact
                }}</el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="base-dialog-footer">
        <el-button
          type="primary"
          :disabled="!submitLoad"
          @click="onSubmit('formData')"
          >{{ operateType.save }}</el-button
        >
        <el-button @click="handleClose">{{ operateType.cancel }}</el-button>
      </span>
    </el-dialog>
    <SysBusinessDetectionItemDialog
      v-if="detectionItemDialogVisible"
      :dialog-form-visible.sync="detectionItemDialogVisible"
      :update-state.sync="updateState"
      :row-data.sync="rowData"
      @close="close"
    />
    <ChoiceProductStandard
      ref="ChoiceProductStandard"
      :dialog-form-visible.sync="choiceDialogFormVisible"
      @getProductStandardList="getProductStandardList"
      @dialogClose="dialogClose"
    />
    <ChoiceMethodStandard
      v-if="choiceMethodStandardVisible"
      ref="ChoiceMethodStandard"
      :choice-method-standard-visible.sync="choiceMethodStandardVisible"
      @setMethodStandard="setMethodStandard"
    />

    <ChoiceUnit
      v-if="unitVisible"
      ref="ChoiceUnit"
      :unit-visible.sync="unitVisible"
      @setUnit="setUnit"
    />
    <ChoiceUserDialog
      v-if="userDialogFormVisible"
      :dialog-form-visible.sync="userDialogFormVisible"
      :detection-people-id-list.sync="formData.detectionPeopleList"
      @choiceUser="choiceUser"
    />
  </div>
</template>

<script>
import {
  create,
  update,
} from "../../../../api/DetectionItemManage/SysBusinessDetectionItem.js";
import { getDictList } from "../../../../api/System/SystemDict";
import { getSampleTree } from "../../../../api/DetectionItemManage/SysBusinessSampleCategory.js";
import SysBusinessDetectionItemDialog from "./SysBusinessDetectionItemDialog.vue";
import ChoiceProductStandard from "./ChoiceProductStandard.vue";
import { getDetectionItem } from "../../../../api/DetectionItemManage/SysBusinessDetectionItem.js";
import { getList } from "../../../../api/DetectionItemManage/SysBusinessProductStandard.js";
import ChoiceMethodStandard from "./ChoiceMethodStandard.vue";
import ChoiceUnit from "./ChoiceUnit.vue";
import ChoiceUserDialog from "../../../../../ModulePermission/components/Permission/ChoiceUserDialog.vue";
// import CommonConfigSelectShowVue from "@/ModuleSystem/components/System/CommonConfigSelectShow.vue";
import _ from "lodash";

export default {
  name: "SysBusinessDetectionItemEdit",
  components: {
    ChoiceProductStandard,
    SysBusinessDetectionItemDialog,
    ChoiceMethodStandard,
    ChoiceUnit,
    ChoiceUserDialog
  },
  props: {
    sysBusinessSampleCategoryId: {
      type: String,
      default: "",
    },
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    editId: {
      type: String,
      default: "",
    },
    treeData: {
      type: Array,
      default: function() {
        return [];
      },
    },
  },
  data() {
    return {
      // 项目类型
      itemTypeOptions: [],
      scopeAccreditationOptions: [],
      detectionPeopleOptions: [],
      // 检测人员
      detectionPeopleList: [],
      validityUnitOptions: [],
      inputValue: "",
      inputVisible: false,
      unitVisible: false,
      qualityIndexPlaceholder: "",
      decisionSymbolList: [],
      updateState: false,
      detectionItemDialogVisible: false,
      loading: false,
      submitLoad: true,
      searchUserLoading: false,
      title: this.$t("SysBusinessDetectionItem.detectionItemInformation"),
      innerDialogFormVisible: false,
      choiceDialogFormVisible: false,
      choiceMethodStandardVisible: false,
      userDialogFormVisible: false,
      userData: [],
      // 编辑使用的行数据
      rowData: {},
      formLabelWidth: "200px",
      // 选择数据
      tabldeDatas: [],
      // 产品标准id
      productStandardId: 0,
      formData: {
        // 项目类型
        itemType: "",
        detectionPeopleList: [],
        detectionPeopleLabelList: "",
        // 有效期单位
        validityUnit: 1,
        scopeAccreditation: 1,
        // 样品有效期
        sampleValidity: "0",
        // 检出限
        detectionLimitList: [],
        id: 0,
        parentId: 0,
        sysBusinessSampleCategoryId: "",
        detectionItemName: "",
        productStandardName: "",
        methodStandard: "",
        methodStandardName: "",
        detectionItemChildEnable: 0,
        productStandardDtos: [],
        itemSubentryDtos: [],
        // 新增数据
        additemSubentryDtos: [],
        // 修改数据
        updateitemSubentryDtos: [],
        // 删除数据
        deleteitemSubentryDtos: [],
      },
      formDataRules: {
        sysBusinessSampleCategoryId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],

        detectionItemName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
      },
      initErrorMsg: {
        sysBusinessSampleCategoryId: "",
        detectionItemName: "",
        productStandardName: "",
        methodStandard: "",
        methodStandardName: "",
      },
      errorMsg: {
        sysBusinessSampleCategoryId: "",
        detectionItemName: "",
        productStandardName: "",
        methodStandard: "",
        methodStandardName: "",
      },
      sampleTreeData: [],
      queryFormProductStandard: {},
      paginationProductStandard: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessDetectionItem() {
      return this.$t("SysBusinessDetectionItem") || {};
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
    // 判定符号
    getDictList({ dictCode: "DecisionSymbol" }).then((res) => {
      this.decisionSymbolList = res.data;
    });
    // 项目类型
    getDictList({ dictCode: "ItemType" }).then((res) => {
      this.itemTypeOptions = res.data;
    });

    // 有效期单位
    getDictList({ dictCode: "ValidityUnit" }).then((res) => {
      this.validityUnitOptions = res.data;
    });

    // 认证认可范围
    getDictList({ dictCode: "ScopeAccreditation" }).then((res) => {
      this.scopeAccreditationOptions = res.data;
    });

    // 样品类别树
    getSampleTree().then((res) => {
      this.sampleTreeData = res.data;
      if (!this.editId) {
        this.$set(
          this.formData,
          "sysBusinessSampleCategoryId",
          this.sysBusinessSampleCategoryId
        );
      }
    });
    this.title = this.$t("SysBusinessDetectionItem.detectionItemInformation");
    if (this.editId) {
      this.getDetectionItemData();
    }
  },
  methods: {
    // 清空检测人员集合
    emptyUser() {
      this.formData.detectionPeopleLabelList = "";
      this.formData.detectionPeopleList = [];
    },
    // 清空单位符号
    emptyUnit() {
      this.formData.unitSymbol = "";
    },
    // 清空方法标准
    emptyMethodStandard() {
      this.formData.methodStandardName = "";
      this.formData.methodStandard = "";
    },
    // 清空产品标准
    emptyProductStandard() {
      this.formData.productStandardName = "";
      this.productStandardId = 0;
    },
    // 选择人员的弹框关闭
    choiceUser(data) {
      if (data !== null && data.length !== 0) {
        this.userData = data;
        this.formData.detectionPeopleLabelList = "";
        this.formData.detectionPeopleList = [];
        for (let i = 0; i < data.length; i++) {
          this.formData.detectionPeopleLabelList += data[i].name + ",";
          this.formData.detectionPeopleList.push(data[i].id);
        }
        this.formData.detectionPeopleLabelList =
          this.formData.detectionPeopleLabelList.substring(
            0,
            this.formData.detectionPeopleLabelList.length - 1
          );
      } else {
        this.formData.detectionPeopleLabelList = "";
        this.formData.detectionPeopleList = [];
      }
    },
    // 选择人员
    selectUser() {
      this.userDialogFormVisible = true;
    },
    // 判断输入内容
    blurTexts(e) {
      if (e.target.value) {
        const boolean = new RegExp("^[1-9][0-9]*$").test(e.target.value);
        if (!boolean) {
          this.$message.warning(
            this.$t("SysBusinessDetectionItem.enterPositiveInteger")
          );
          e.target.value = "";
        }
      }
    },
    handleInputConfirm() {
      if (this.formData.detectionLimitList == null) {
        // 如果没有数据，设为数组
        this.formData.detectionLimitList = [];
      }
      if (this.inputValue !== "" && this.inputValue !== null) {
        // 添加一个键值对，值为input框输入的内容
        this.formData.detectionLimitList.push({ name: this.inputValue });
      }
      // 关闭input框
      this.inputVisible = false;
      // 关闭后清空input框
      this.inputValue = "";
    },
    // 点击出现输入框
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleCloseTag(tag) {
      this.formData.detectionLimitList.splice(
        this.formData.detectionLimitList.indexOf(tag),
        1
      );
    },

    setUnit(val) {
      this.formData.unitSymbol = val.unitName;
    },
    selectUnit() {
      this.unitVisible = true;
    },
    decisionSymbolChange() {
      if (this.formData.decisionSymbol === "∈") {
        this.qualityIndexPlaceholder = this.$t(
          "SysBusinessDetectionItem.connectionIntervalExample"
        );
      } else {
        this.qualityIndexPlaceholder = "";
      }
    },
    // 编辑检测分项
    editUnit(data) {
      this.rowData = data;
      this.updateState = true;
      this.detectionItemDialogVisible = true;
    },
    // 表格复选框
    handleSelectionChange(val) {
      this.tabldeDatas = val;
    },
    // 删除检测分项
    deleteDetection() {
      for (let i = 0; i < this.tabldeDatas.length; i++) {
        // 如果id不等于空就是需要删除数据库的数据
        if (this.tabldeDatas[i].id !== null && this.tabldeDatas[i].id !== "") {
          if (this.formData.deleteitemSubentryDtos === null) {
            this.formData.deleteitemSubentryDtos = [];
          }
          this.formData.deleteitemSubentryDtos.push(this.tabldeDatas[i]);
          // 删除页面表格显示的数据
          if (
            this.formData.itemSubentryDtos !== null &&
            this.formData.itemSubentryDtos.length !== 0
          ) {
            for (let p = 0; p < this.formData.itemSubentryDtos.length; p++) {
              if (
                this.formData.itemSubentryDtos[p].id === this.tabldeDatas[i].id
              ) {
                this.formData.itemSubentryDtos.splice(p, 1);
              }
            }
          }
        } else {
          // 删除存储在新增里的数据根据serialNumber
          if (
            this.formData.additemSubentryDtos !== null &&
            this.formData.additemSubentryDtos.length !== 0
          ) {
            for (let p = 0; p < this.formData.additemSubentryDtos.length; p++) {
              if (
                this.formData.additemSubentryDtos[p].serialNumber ===
                this.tabldeDatas[i].serialNumber
              ) {
                this.formData.additemSubentryDtos.splice(p, 1);
              }
            }
          }
          if (
            this.formData.itemSubentryDtos !== null &&
            this.formData.itemSubentryDtos.length !== 0
          ) {
            for (let p = 0; p < this.formData.itemSubentryDtos.length; p++) {
              if (
                this.formData.itemSubentryDtos[p].serialNumber ===
                this.tabldeDatas[i].serialNumber
              ) {
                this.formData.itemSubentryDtos.splice(p, 1);
              }
            }
          }
        }
      }
    },
    // 复制检测分项
    copyDetection() {
      for (let i = 0; i < this.tabldeDatas.length; i++) {
        var data = JSON.parse(JSON.stringify(this.tabldeDatas[i]));
        data.id = 0;
        this.formData.itemSubentryDtos.push(data);
        if (this.formData.additemSubentryDtos === null) {
          this.formData.additemSubentryDtos = [];
        }
        this.formData.additemSubentryDtos.push(data);
      }
    },
    // 新增检测分项
    addDetection() {
      this.detectionItemDialogVisible = true;
    },
    // 关闭新增检测项目分项弹框触发事件
    close(data) {
      // 编辑的整好是新增的数据
      if (data.id === 0 && this.updateState) {
        // 在存储新增数据的集合里替换数据
        for (let i = 0; i < this.formData.additemSubentryDtos.length; i++) {
          if (
            this.formData.additemSubentryDtos[i].serialNumber ===
            data.serialNumber
          ) {
            this.formData.additemSubentryDtos[i] = data;
          }
        }

        // 替换页面表格中的数据
        for (let i = 0; i < this.formData.itemSubentryDtos.length; i++) {
          if (
            this.formData.itemSubentryDtos[i].serialNumber === data.serialNumber
          ) {
            this.formData.itemSubentryDtos[i] = data;
          }
        }
      } else if (data.id === 0) {
        // 新增的数据存入additemSubentryDtos

        if (
          this.formData.additemSubentryDtos === null ||
          this.formData.additemSubentryDtos.length === 0
        ) {
          data.serialNumber = 0;
          this.formData.additemSubentryDtos = [];
        } else {
          data.serialNumber = this.formData.additemSubentryDtos.length + 1;
        }
        this.formData.additemSubentryDtos.push(data);
        // 存入表格展示数据
        this.formData.itemSubentryDtos.push(data);
      }

      // 编辑的数据
      if (this.updateState && data.id !== 0) {
        if (this.formData.updateitemSubentryDtos === null) {
          this.formData.updateitemSubentryDtos = [];
        }
        if (
          this.formData.updateitemSubentryDtos !== null &&
          this.formData.updateitemSubentryDtos.length !== 0
        ) {
          var flag = false;
          for (
            let i = 0;
            i < this.formData.updateitemSubentryDtos.length;
            i++
          ) {
            if (this.formData.updateitemSubentryDtos[i].id === data.id) {
              this.formData.updateitemSubentryDtos[i] = data;
              flag = true;
            }
          }
          if (!flag) {
            this.formData.updateitemSubentryDtos.push(data);
          }
        } else {
          this.formData.updateitemSubentryDtos.push(data);
        }

        for (let i = 0; i < this.formData.itemSubentryDtos.length; i++) {
          if (this.formData.itemSubentryDtos[i].id === data.id) {
            this.formData.itemSubentryDtos[i] = data;
          }
        }
      }
      this.rowData = {};
      this.updateState = false;
    },

    // 获取检测项目数据
    getDetectionItemData() {
      getDetectionItem({ id: this.editId }).then((res) => {
        this.formData = res.data;
        this.productStandardId = res.data.productStandardId;
      });
    },

    // 关闭弹窗触发
    dialogClose() {
      this.formData.productStandardName =
        this.$refs.ChoiceProductStandard.choiceData.productStandardName;
      this.productStandardId = this.$refs.ChoiceProductStandard.choiceData.id;
    },
    // 选择标准方法
    selectProductStandard() {
      this.choiceDialogFormVisible = true;
      this.getProductStandardList();
    },
    // 查询标准方法
    getProductStandardList() {
      this.queryFormProductStandard =
        this.$refs.ChoiceProductStandard.queryForm;
      this.paginationProductStandard =
        this.$refs.ChoiceProductStandard.pagination;
      getList(this.queryFormProductStandard, this.paginationProductStandard)
        .then((res) => {
          this.$refs.ChoiceProductStandard.list = res.data.dataList
            ? res.data.dataList
            : [];
          this.$refs.ChoiceProductStandard.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 查询方法标准
    selectMethodStandard() {
      this.choiceMethodStandardVisible = true;
    },
    // 查询方法标准
    setMethodStandard(val) {
      this.formData.methodStandardName = val.methodStandardName;
      this.formData.methodStandard = val.methodStandardNum;
    },
    cancel() {
      this.$emit("update:editId", "");
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    // 关闭弹框
    handleClose() {
      this.$emit("input", false);
      this.$emit("update:editId", "");
      this.innerDialogFormVisible = false;
    },
    // 关闭弹框动画结束
    closedDialog() {
      // 动画结束后在清空表单 防止页面闪烁
      this.$refs.formData.resetFields();
      this.$emit("input", false);
      this.$parent.getList();
    },

    onSubmit: _.throttle(function() {
      if (this.submitLoad) {
        if (!this.formData.sampleValidity) {
          this.formData.sampleValidity = "0";
        }
        this.submitLoad = false;
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);

            var data = {
              detectionItemId: this.formData.id,
              productStandardId: this.productStandardId,
            };
            this.formData.productStandardDtos = [];
            this.formData.productStandardDtos.push(data);
            // 如果没有启用项目分项,清空itemSubentryDtos
            if (this.formData.detectionItemChildEnable === 0) {
              this.formData.itemSubentryDtos = [];
            } else if (this.formData.detectionItemChildEnable === 1) {
              this.formData.qualityIndex = "";
              this.formData.decisionSymbol = "";
              this.formData.unitSymbol = "";
            }
            if (this.editId !== null && this.editId !== "") {
              update(this.formData)
                .then((res) => {
                  if (res.code === 200) {
                    this.submitLoad = true;
                    this.closeDialog();
                  } else {
                    this.submitLoad = true;

                    this.showValidateMsgCheck(res);
                  }
                  this.submitLoad = true;

                  this.loading = false;
                })
                .catch(() => {
                  this.submitLoad = true;

                  this.loading = false;
                });
            } else {
              create(this.formData)
                .then((res) => {
                  if (res.code === 200) {
                    this.submitLoad = true;

                    this.closeDialog();
                  } else {
                    this.submitLoad = true;

                    this.showValidateMsgCheck(res);
                  }
                  this.submitLoad = true;

                  this.loading = false;
                })
                .catch(() => {
                  this.submitLoad = true;

                  this.loading = false;
                });
            }
          } else {
            this.submitLoad = true;
            return false;
          }
        });
      }
    }, 2000),
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg(
          "sysBusinessSampleCategoryId",
          this.errorMsg,
          res.msg
        );
        this.Helper.showValidateMsg(
          "detectionItemName",
          this.errorMsg,
          res.msg
        );
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.emptyProductStandard {
  width: 30px;
  color: white;
  font-size: 20px;
  height: 32px;
  text-align: center;
  border-radius: 5px;
  border: 1px silver;
  background-color: red;
  position: absolute;
  top: 0px;
  right: 0px;
}

.selectProductStandard {
  width: 30px;
  color: white;
  font-size: 20px;
  height: 32px;
  text-align: center;
  border-radius: 5px;
  border: 1px silver;
  background-color: #4787ff;
  position: absolute;
  top: 0px;
  right: 0px;
}

.system-detectionitem-form {
  // padding-bottom: 24px;
  .relay-only-value {
    margin-bottom: 2px;
  }
  .default-form-item {
    display: block;
    height: 84px;
  }
}

.add {
  width: 30px;
  color: white;
  font-size: 20px;
  height: 32px;
  text-align: center;
  border-radius: 5px;
  border: 1px silver;
  background-color: #4787ff;
  margin-left: 10px;
  float: left;
  position: absolute;
  top: 0px;
  left: 121px;
}
.delete {
  background-color: rgb(194, 194, 194);
  left: 170px;
  font-size: 12px;
  border-radius: 10px;
}
</style>

